package com.pimwi.service
{
  import com.pimwi.dp.ContactDP;
  import com.pimwi.dp.GroupDP;
  import com.pimwi.model.ModelLocator;
  import com.pimwi.vo.Contact;
  import com.pimwi.vo.Group;
  import com.pimwi.vo.User;
  import com.pimwi.vo.request.LoginUserRequest;
  
  import mx.collections.ArrayCollection;
  import mx.controls.Alert;
  import mx.rpc.events.ResultEvent;
  
  public class UserService extends BaseService
  {
    public function UserService()
    {
      super();
    }

    /***********/
    /** LOGIN **/
    /***********/

    public function login(request:LoginUserRequest):void
    {
      _service.login.addEventListener(ResultEvent.RESULT, loginResultHandler);
      _service.login(request);
    }

    private function loginResultHandler(event:ResultEvent):void
    {
      _service.login.removeEventListener(ResultEvent.RESULT, loginResultHandler);

      var locator:ModelLocator = ModelLocator.getInstance();

      var response:Object = event.result;

      if (response.errorMessage != "")
      {
        Alert.show(response.errorMessage, "Connexion impossible");
      }
      else
      {
        var user:User = new User();
        user.id = response.user.id;
        user.firstName = response.user.firstName;
        user.lastName = response.user.lastName;
        user.userName = response.user.userName;
        user.password = response.user.password;
        locator.userDP.user = user;
        locator.userConnected = true;

        var data:Object = null;

        var retrievedContacts:ArrayCollection = response.userContacts; // list of Contact
        var userContacts:ArrayCollection = new ArrayCollection(); // list of ContactDP
        var contact:Contact = null;
        for each (data in retrievedContacts)
        {
          contact = new Contact(data.id, data.userId, data.firstName, data.lastName, data.address, data.city, data.email, data.phone, data.groupId);
          var contactDP:ContactDP = new ContactDP(contact);
          userContacts.addItem(contactDP);
        }
        locator.userContacts = userContacts;
        locator.filteredContacts = userContacts;

        var retrievedGroups:ArrayCollection = response.userGroups; // list of Group
        var userGroups:ArrayCollection = new ArrayCollection(); // list of GroupDP
        var group:Group = null;

        var noneGroup:Group = new Group(0, 0, "N/A");
        userGroups.addItemAt(new GroupDP(noneGroup), 0);

        for each (data in retrievedGroups)
        {
          group = new Group(data.id, data.userId, data.name);
          var groupDP:GroupDP = new GroupDP(group);
          userGroups.addItem(groupDP);
        }
        locator.userGroups = userGroups;

        var allGroupsFilter:Group = new Group(-1, 0, "Tous");
        var filterGroups:ArrayCollection = new ArrayCollection();
        filterGroups.addItem(new GroupDP(allGroupsFilter));
        
        for each (var _groupDP:GroupDP in userGroups)
        {
          filterGroups.addItem(_groupDP);
        }
        locator.filterGroups = filterGroups;
      }
    }

  }
}